//
//  NYContactViewController.m
//  NiuWaJiaoYu
//
//  Created by MinLison on 2018/1/6.
//  Copyright © 2018年 niuwaketang. All rights reserved.
//

#import "NYContactViewController.h"

#import "NYContactHeadReusableView.h"
#import "NYContactFooterReusableView.h"

#import "NYContactCollectionCell.h"

//models
#import "NYContactModel.h"

//request
#import "NYContactRequest.h"


@interface NYContactViewController ()

@property (nonatomic, strong) NYContactRequest *request;
@property (nonatomic, copy) NSString *addressString;

@end

@implementation NYContactViewController

#pragma mark - Life Cycle Methods

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initUI];
}
#pragma mark - Intial Methods
- (void)initUI {
    
    
    [self loadData];
}

- (void)initCollectionView {
    
    [super initCollectionView];

    [self.collectionView registerClass:[NYContactHeadReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:[NYContactHeadReusableView className]];
    [self.collectionView registerClass:[NYContactFooterReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:[NYContactFooterReusableView className]];

//    [self.collectionView registerClass:[NYContactCollectionCell class] forCellWithReuseIdentifier:[NYContactCollectionCell className]];
}
- (void)configNimbus {
    [super configNimbus];
    [self.collectionViewActions forwardingTo:self.controllerView];
}
#pragma mark - Network Methods
- (void)loadData {
    [self setLoading:YES animation:YES];
    [self configNimbus];

    @weakify(self);
    [self.request startWithSuccess:^(__kindof BaseRequest * _Nonnull request, __kindof NYContactModel * _Nonnull data) {
        
        @strongify(self);
        [self.collectionViewModel addObjectsFromArray:data.contact];
        self.addressString = [NSString stringWithFormat:@"%@\n%@\n%@",data.telephone, data.customerService, data.address];
        [self.collectionView reloadData];
        [self setSuccess];
    } failed:^(__kindof BaseRequest * _Nonnull request, NSError * _Nonnull error) {
        
        [self setError:error];
    }];

}
#pragma mark - Target Methods

#pragma mark - Public Methods

#pragma mark - Private Method

#pragma mark - External Delegate

/// MARK: - collectionViewModel Delegate
//- (UICollectionViewCell *)collectionViewModel:(NICollectionViewModel *)collectionViewModel cellForCollectionView:(UICollectionView *)collectionView atIndexPath:(NSIndexPath *)indexPath withObject:(id)object {
//    UICollectionViewCell *cell = [super collectionViewModel:collectionViewModel cellForCollectionView:collectionView atIndexPath:indexPath withObject:object];
//    return cell;
//}
- (UICollectionReusableView *)collectionViewModel:(NICollectionViewModel *)collectionViewModel collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    if (kind == UICollectionElementKindSectionHeader) {
        NYContactHeadReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:[NYContactHeadReusableView className] forIndexPath:indexPath];
        return header;
    } else if (kind == UICollectionElementKindSectionFooter) {
        NYContactFooterReusableView *footer = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:[NYContactFooterReusableView className] forIndexPath:indexPath];
        if (self.addressString.length > 0) {
            footer.title = self.addressString;
        }
        return footer;
    }
    return nil;
}
#pragma mark - Setter Getter Methods
- (NYContactRequest *)request  {
    if (!_request) {
        _request = [NYContactRequest requestWithParams:nil];
    }
    return _request;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
